<template>
    <div
        class="text-message"
        :class="{
        left: float == 'left',
        right: float == 'right',
        'max-width': !fullWidth,
        }"
    >
        <div v-if="arrow" class="arrow"></div>
        <pre v-html="html" />
    </div>
</template>

<script>
import { reactive, toRefs, onMounted } from 'vue'
import { textReplaceLink } from '/@/views/chat/utils/functions.js'
import { textReplaceEmoji } from '/@/views/chat/utils/emojis.js'
export default {
    name: 'TextMessage',
    props: {
        content: {
          type: [String, Number],
          default: '',
        },
        float: {
          type: String,
          default: 'left',
        },
        fullWidth: {
          type: Boolean,
          default: true,
        },
        arrow: {
          type: Boolean,
          default: false,
        }
    },
    setup(props) {
        const { content, float } = toRefs(props)
        const state = reactive({
            html: ''
        })
        onMounted(() => {
            const text = textReplaceLink(
                content.value,
                float.value == 'right' ? '#ffffff' : 'rgb(9 149 208)'
            )
            state.html = textReplaceEmoji(text)
        })
        return {
            ...toRefs(state)
        }
    }
}
</script>

<style lang="scss" scoped>
.text-message {
  position: relative;
  min-width: 30px;
  min-height: 30px;
  border-radius: 5px;
  padding: 5px;
  .arrow {
    position: absolute;
    width: 0;
    height: 0;
    font-size: 0;
    border: 5px solid;
    top: 6px;
    left: -10px;
  }
  &.max-width {
    max-width: calc(100% - 50px);
  }
  &.left {
    color: #3a3a3a;
    background: #f5f5f5;
    .arrow {
      border-color: transparent #f5f5f5 transparent transparent;
    }
  }
  &.right {
    color: #3a3a3a;
    background: #daf3fd;
    .arrow {
      right: -10px;
      left: unset;
      border-color: transparent transparent transparent #daf3fd;
    }
  }
  pre {
    white-space: pre-wrap;
    overflow: hidden;
    word-break: break-word;
    word-wrap: break-word;
    font-size: 14px;
    padding: 3px 10px;
    font-family: 'Microsoft YaHei';
    line-height: 25px;
  }
}
</style>